//询价单新增/编辑/复制

interface IInquiryAddViewModel extends ng.IScope {
    /**
     * 编辑询价发布是否隐藏
     */
    saveDis: boolean;
    /**
     * 总询价是否可以改动
     */
    editInquiryDis: boolean;
    /**
     * 显示总询价编号
     */
    inquiryIdShow: boolean;
    /**
     * 子询价单备注（编辑重下）
     */
    remarksShow: boolean;
    /**
     * 标题
     */
    inquiryName: string;
    /**
     * 子询价单列表视图
     */
    inquiryChildViewList: Array<InquiryChildList>;
    /**
     * 总询价单编号
     */
    inquiryId: string;
    /**
     * 紧急程度
     */
    urgency: string;
    /**
     * 紧急程度单位
     */
    urgencyUnit: string;
    /**
    *紧急程度单位下拉
    */
    urgencyUnitDropDown: ValueListItemResponse[];
    /**
     * 商务专员id
     */
    businessOfficerId: string;
    /**
     * 商务专员
     */
    businessOfficerName: string;
    /**
     * 商务专员下拉列表
     */
    businessOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 计划专员
     */
    plannerName: string;
    /**
    *客服专员编号
    */
    customerServiceOfficerId: string;
    /**
     * 客服专员
     */
    customerServiceOfficerName: string;
    /**
     * 客服专员下拉列表
     */
    serviceOfficerIdDropDown: EmployeeListItemResponse[];
    /**
    *调度专员编号
    */
    dispatchOfficerId: string;
    /**
     * 调度专员
     */
    dispatchOfficerName: string;
    /**
     *调度专员下拉列表
     */
    dispatchOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 客户单位编号
     */
    clientId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 发货员编号
     */
    consignorId: string;
    /**
    *发货员姓名
    */
    consignorName: string;
    /**
    *发货员号码
    */
    consignorPhone: string;
    /**
     * 总询价内容
     */
    content: string;
    /**
     *发货地省
    */
    startProvince: string;
    /**
     *发货地省下拉
    */
    startProvinceDropDown: Array<AreaItem>;
    /**
     *发货地市
     */
    startCity: string;
    /**
     *发货地市下拉
     */
    startCityDropDown: Array<AreaItem>;
    /**
     *发货地区
    */
    startArea: string;
    /**
     *发货地区下拉
    */
    startAreaDropDown: Array<AreaItem>;
    /**
     *发货地详细地址
    */
    startAddress: string;
    /**
     *送货地省
    */
    endProvince: string;
    /**
     *送货地省下拉
    */
    endProvinceDropDown: Array<AreaItem>;
    /**
     *送货地市
    */
    endCity: string;
    /**
     *送货地市下拉
    */
    endCityDropDown: Array<AreaItem>;
    /**
     *送货地区
    */
    endArea: string;
    /**
     *送货地区下拉
    */
    endAreaDropDown: Array<AreaItem>;
    /**
     *送货地详细地址
    */
    endAddress: string;
    /**
     *预计发货时间
    */
    plannedArrivalTime: string;
    /**
     *预计到货时间
    */
    plannedDeliveryTime: string;
    /**
     *公里数
    */
    mileage: number;
    /**
     *货物类别编号
    */
    goodsTypeId: string;
    /**
     *货物类别下拉
    */
    goodsTypeDropDown: Array<GoodsTypeListItemResponse>;
    /**
     *货物编号
    */
    goodsId: string;
    /**
     * 货物名称
     */
    goodsName:string;
    /**
     *货物名称下拉
    */
    goodsNameDropDown: Array<GoodsListItemResponse>;
    /**
     *货物数量
    */
    goodsQuantity: number;
    /**
     * 货物数量单位
     */
    goodsQuantityUnit: string;
    /**
     *货物数量单位下拉
    */
    goodsQuantityUnitDropDown: ValueListItemResponse[];
    /**
     *所需车型下拉
    */
    carTypeDropDown: ValueListItemResponse[];
    /**
     *所需车长下拉
    */
    carLengthDropDown: ValueListItemResponse[];
    /**
     * 承运方式
     */
    carrierCategory: string;
    /**
     *承运方式下拉
    */
    carrierCategoryDropDown: ValueListItemResponse[];
    /**
     * 所需车型
     */
    carType: string;
    /**
     * 所需车长
     */
    carLength: string;
    /**
     *装车效果
    */
    loadingEffect: string;
    /**
     * 附件上传组件
     */
    attachmentUploader: any;
    /**
     * 附件备注
     */
    attachmentDetails: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 客户自动补全
     */
    searchClient: (value: string) => any;
    /**
     * 选择客户单位事件
     */
    clientSelect: (selected: angucomplete<ClientListItemResponse>) => void;
    /**
     * 发货计划人补全事件
     */
    searchConsignor: (value: string) => void;
    /**
     * 货物事件
     */
    goodsTypeSelect: () => void;

    /**
     * 选择发货计划人事件
     */
    consignorSelect: (selected: angucomplete<CustomerRepresentativeListItemResponse>) => void;
    /**
     * 是否显示子询价
     */
    inquiryChildShow: boolean;
    /**
     * 子询价单新增
     */
    addInquiryChild: () => void;
    /**
     * 删除子询价单
     */
    delInquiryChild: (index: number) => void;
    /**
     * 添加/复制/编辑询价发布
     */
    inquirySave: () => void;
    /**
     * 是否选中
     */
    ischecked: boolean;
    /**
    * 货物名称自动补全事件
    */
    onGoodsNameAutoComplate: (value: string) => any;
    /**
     * 货物名称选中事件
     */
    onGoodsNameSelected: (selected: angucomplete<GoodsListItemResponse>) => void;
    /**
     * 是否禁用
     */
    isInquiryDis:boolean;

}

interface InquiryChildList {
    /**
     * 标志符
     */
    index: number;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliveryAddress: string;
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    goodsQuantity: string;
    /**
     * 预计发货时间
     */
    plannedArrivalTime: string;
    /**
     * 预计到货时间
     */
    plannedDeliveryTime: string;
    /**
     * 所需车型
     */
    carType: string;
    /**
     * 所需车长
     */
    carLength: string;

}

class InquiryAddController {
    gridApi: uiGrid.IGridApiOf<InquiryListItemResponse>;
    uploaderList: Array<attachmentList>;
    baseImageUrl: string;
    /**
     * 询价单类型
     */
    name: string;
    inquiryId: string;
    /**
     * 子询价单列表数据
     */
    inquiryChildListData: Array<InquiryChildListResponse>;

    constructor(private $scope: IInquiryAddViewModel, private customerRepresentativeService: ICustomerRepresentativeService, private $state: angular.ui.IStateService, private $location: ng.ILocationService, private $http: ng.IHttpService,
        private inquiryService: IInquiryService, private clientService: IClientService, private employeeService: IEmployeeService, private goodsService: IGoodsService, private goodsTypeService: IGoodsTypeService,
        private areaService: IAreaService, private valueService: IValueService, private routeService: routeService, private $stateParams: any, private $ngBootbox: BootboxService, private commonService: ICommonService,private $q: ng.IQService,private ossUploaderService:ossUploaderService) {
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        this.uploaderList = [];
        this.init();
    }

    init(): void {
        this.name = this.$location.search().name;
        this.inquiryId = this.$location.search().id;
        if (window.localStorage.getItem("loginData").toString()) {
            this.$scope.plannerName = JSON.parse(window.localStorage.getItem("loginData").toString()).realName;
        };
        this.$scope.inquiryChildViewList = [];
        this.inquiryChildListData = [];

        this.$scope.editInquiryDis = false;
        this.$scope.inquiryIdShow = true;
        this.$scope.inquiryChildShow = false;
        this.$scope.remarksShow = false;
        this.$scope.inquiryName = "询价单新增";
        this.$scope.isInquiryDis = false;

        /**
         * 自动补全事件
         */
        this.$scope.searchClient = this.searchClient;
        this.$scope.searchConsignor = this.searchConsignor;
        this.$scope.onGoodsNameAutoComplate = this.onGoodsNameAutoComplate;
        this.$scope.onGoodsNameSelected = this.onGoodsNameSelected;

        /**
         * 选择事件
         */
        this.$scope.clientSelect = this.clientSelect;
        this.$scope.goodsTypeSelect = this.goodsTypeSelect;
        this.$scope.consignorSelect = this.consignorSelect;

        this.$scope.addInquiryChild = this.addInquiryChild;
        this.$scope.delInquiryChild = this.delInquiryChild;

        this.loadUrgencyUnitDropDown();
        this.loadBusinessOfficerDropDown();
        this.loadServiceOfficerIdDropDown();
        this.loadDispatchOfficerDropDown();
        this.loadGoodsQuantityUnitDropDown();
        this.loadCarTypeDropDown();
        this.loadCarLengthDropDown();
        this.loadCarrierCategoryDropDown();
        $('#opendataTime').click(() => {
            $('#dataTime').datetimepicker('show');
        });

        $('#opendataTimeEnd').click(() => {
            $('#dataTimeEnd').datetimepicker('show');
        });

        if (this.inquiryId != null) {
            this.inquiryService.getDetail(this.inquiryId).then(res => {
                if (res.data.inquiryChildList[0].mileage == 0) {
                    res.data.inquiryChildList[0].mileage = null;
                };
                this.$scope.urgency = String(res.data.urgency);
                this.$scope.urgencyUnit = res.data.urgencyUnit;
                if (this.name != "copy") {
                    this.$scope.plannerName = res.data.plannerName;
                }

                this.$scope.businessOfficerId = res.data.businessOfficerId;
                this.$scope.businessOfficerName = res.data.businessOfficerName;
                this.$scope.customerServiceOfficerId = res.data.customerServiceOfficerId;
                this.$scope.customerServiceOfficerName = res.data.customerServiceOfficerName;
                this.$scope.dispatchOfficerId = res.data.dispatchOfficerId;
                this.$scope.dispatchOfficerName = res.data.dispatchOfficerName;
                this.$scope.clientId = res.data.clientId;
                this.$scope.consignorId = res.data.consignorId;
                this.$scope.clientName = res.data.clientName;
                this.$scope.consignorName = res.data.consignorName;
                this.$scope.consignorPhone = res.data.consignorPhone;
                this.$scope.startProvince = res.data.inquiryChildList[0].startProvince;
                this.$scope.startCity = res.data.inquiryChildList[0].startCity;
                this.$scope.startArea = res.data.inquiryChildList[0].startArea;
                this.$scope.startAddress = res.data.inquiryChildList[0].startAddress;
                this.$scope.endProvince = res.data.inquiryChildList[0].endProvince;
                this.$scope.endCity = res.data.inquiryChildList[0].endCity;
                this.$scope.endArea = res.data.inquiryChildList[0].endArea;
                this.$scope.endAddress = res.data.inquiryChildList[0].endAddress;
                this.$scope.plannedArrivalTime = res.data.inquiryChildList[0].plannedArrivalTime;
                this.$scope.plannedDeliveryTime = res.data.inquiryChildList[0].plannedDeliveryTime;
                this.$scope.mileage = res.data.inquiryChildList[0].mileage;
                this.$scope.goodsTypeId = res.data.inquiryChildList[0].goodsTypeId;
                this.$scope.goodsId = res.data.inquiryChildList[0].goodsId;
                this.$scope.goodsName = res.data.inquiryChildList[0].goodsName;
                this.$scope.goodsQuantity = res.data.inquiryChildList[0].goodsQuantity;
                this.$scope.goodsQuantityUnit = res.data.inquiryChildList[0].goodsQuantityUnit;
                this.$scope.carrierCategory = res.data.inquiryChildList[0].carrierCategory;
                this.$scope.carType = res.data.inquiryChildList[0].carType;
                this.$scope.carLength = res.data.inquiryChildList[0].carLength;
                this.$scope.loadingEffect = res.data.inquiryChildList[0].loadingEffect;
                this.$scope.content = res.data.content;
                this.$scope.attachmentDetails = res.data.attachmentDetails;
                this.$scope.remarks = res.data.inquiryChildList[0].remarks;


                this.goodsTypeDropDown();
                // this.loadGoodsDropDown(this.$scope.goodsTypeId);
                /**
                 * 附件信息(编辑重下显示)
                 */
                if (this.name == "edit") {
                    if (res.data.attachment.length <= 0) {
                        return;
                    }
                    else {
                        // res.data.attachment.forEach((item, index) => {
                        //     this.uploaderList.push(item);
                        //     this.showFileItem(item, this.$scope.attachmentUploader);
                        // });
                        res.data.attachment.forEach((item, index) => {
                            this.uploaderList.push({
                                attachmentId: item[0],
                                attachmentName: item[1],
                                attachmentPath: item[2],
                                attachmentTag: item[3],
                            });
                            
                            this.$scope.attachmentUploader.addByPath(item[2],{id:item[0],name:item[1]})
                            // this.commonService.showFileItem({
                            //     attachmentId: item[0],
                            //     attachmentName: item[1],
                            //     attachmentPath: item[2],
                            //     attachmentTag: item[3],
                            // }, this.$scope.attachmentUploader);
                        });
                    }
                }
            });

            /**
            * 编辑重下
            */
            if (this.name == "edit") {
                this.$scope.editInquiryDis = true;
                this.$scope.inquiryIdShow = true;
                this.$scope.remarksShow = true;
                this.$scope.inquiryName = "编辑重下";
                this.$scope.ischecked = true;
                /**
                 * 询价发布
                 */
                this.$scope.inquirySave = this.inquirySaveEdit;
            }
            else if (this.name == "copy") {
                this.$scope.inquiryName = "询价单新增";
                this.$scope.inquiryChildShow = true;
                this.$scope.inquirySave = this.inquirySave;
                this.$scope.ischecked = true;
            }
        }
        else if (this.name == "Add") {
            this.$scope.inquiryName = "询价单新增";

            this.$scope.inquiryChildShow = true;
            this.$scope.inquirySave = this.inquirySave;
            this.$scope.remarksShow = false;
            this.$scope.ischecked = false;
        }
      
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachmentUploader= new OssFile(this.$scope,this.$scope.attachmentUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});
        this.$scope.attachmentUploader.onSuccess=(it)=>{
            console.log('onsuccess',it);
            this.onSuccessItem(it.fileId, it.fileName,it.imgSrc,1,it._file)

        }
    }

    /**
     * 客户单位自动补全
     */
    searchClient = (value: string) => {
        return this.clientService.getList(value, "","", "", "", "", 0, 5).then((res) => {
            return res.data.data;
        });
    }

    /**
     * 选择客户单位
     */
    clientSelect = (selected: angucomplete<ClientListItemResponse>) => {
        if (selected) {
            this.$scope.clientId = selected.description.id;
            this.$scope.goodsTypeId = "";
            this.$scope.goodsTypeDropDown = [];
            this.$scope.goodsId = "";
            this.$scope.goodsNameDropDown = [];
            this.goodsTypeDropDown();
            this.$scope.$broadcast('angucomplete-alt:clearInput', 'goodsId');
            $("#consignor_value").val("");
            this.$scope.consignorPhone = "";

        }

    }
    /**
     * 发货计划人补全事件
     */
    searchConsignor = (value: string) => {
        return this.customerRepresentativeService.getList(this.$scope.clientId, value, "", 0, 5).then(res => {
            return res.data.data;

        })

    }

    /**
     * 选择发货计划人事件
     */
    consignorSelect = (selected: angucomplete<CustomerRepresentativeListItemResponse>) => {
        if (selected) {
            this.$scope.consignorId = selected.description.id;
            this.$scope.consignorPhone = selected.description.phoneNumber;

        } else if (!selected) {

            this.$scope.consignorPhone = "";

        } else if (this.$scope.consignorPhone == "" || this.$scope.consignorPhone == undefined) {
            $("#consignor_value").val("");
            this.$scope.consignorId = "";
        }
    }
    /**
     * 获取货物类别下拉
     */
    goodsTypeDropDown = () => {
        this.goodsTypeService.getList(this.$scope.clientId, "", 0, -1).then((res) => {
            this.$scope.goodsTypeDropDown = res.data.data;
            if (this.$scope.clientId == undefined || this.$scope.clientId == "") {
                this.$scope.goodsTypeDropDown = [];
                this.$scope.goodsTypeId = "";
            }
        });
    }

    /**
     * 选择货物类型事件
     */
    goodsTypeSelect = () => {
        // this.loadGoodsDropDown(this.$scope.goodsTypeId);
        this.$scope.$broadcast('angucomplete-alt:clearInput', 'goodsId');
    }

    /**
     * 货物名称自动补全事件
     */
    onGoodsNameAutoComplate = (value: string) => {
        // console.log($("#goodsId_value").val());
        var deferred = this.$q.defer();
        if (!this.$scope.clientId || !this.$scope.goodsTypeId) {
            // deferred.resolve("well done!");
        } else {
            return this.goodsService.getList(this.$scope.clientId, value, this.$scope.goodsTypeId, 0, 30).then(res => {
                return res.data.data;
            })
        }
        return deferred.promise;
        //console.log(this.$q.reject)
    }

    /**
     * 货物名称选中事件
     */
    onGoodsNameSelected = (selected: angucomplete<GoodsListItemResponse>) => {
        if (selected) {
            this.$scope.goodsName = selected.description.name;
            this.$scope.goodsId = selected.description.id;
            $("#goodsId_value").val(selected.description.name);
        } else {
            this.$scope.goodsName = "";
            this.$scope.goodsId = "";
        }

    }


    /**
     * 获取货物名称下拉
     */
    // loadGoodsDropDown = (goodsTypeId: string) => {
    //     this.goodsService.getList(this.$scope.clientId, "", goodsTypeId, 0, -1).then(result => {
    //         this.$scope.goodsNameDropDown = result.data.data;
    //         if (this.$scope.goodsTypeId == undefined || this.$scope.goodsTypeId == "") {
    //             this.$scope.goodsNameDropDown = [];
    //             this.$scope.goodsId = "";
    //         };
    //     });
    // }

    /**
     * 子询价单新增
     */
    addInquiryChild = () => {
        if (!$("#inquiryForm").valid()) {  this.$ngBootbox.alert('请填写完整必填的信息！'); return };
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return; };
        if (this.name == "Add" || this.name == "copy") {
            if (this.$scope.goodsQuantityUnit > "2" && this.$scope.goodsQuantityUnit < "7") {
                if (!(/(^[0-9]\d*$)/.test(String(this.$scope.goodsQuantity)))) {
                    this.$ngBootbox.alert("货物数量请填写正整数");
                    return
                }
            }
            if (!$("#inquiryForm").valid()) { return; }
            if ($("#client_value").val() == "") {
                this.$ngBootbox.alert("请填写客户单位"); return;
            };

            let index = this.$scope.inquiryChildViewList.length + 1;
            //子询价（显示）
            let inquiryChildListData = {
                index: index++,
                shipAddress: $(".province option:selected")[0].innerHTML + $(".city option:selected")[0].innerHTML + $(".area option:selected")[0].innerHTML,
                deliveryAddress: $(".province option:selected")[1].innerHTML + $(".city option:selected")[1].innerHTML + $(".area option:selected")[1].innerHTML,
                // goodsName: $("#inquiryAdd_goodsName option:selected").text(),
                goodsName: $("#goodsId_value").val(),
                goodsQuantity: this.$scope.goodsQuantity + $("#inquiryAdd_goodsQuantityUnit option:selected").text(),
                plannedArrivalTime: $("#dataTime").val(),
                plannedDeliveryTime: $("#dataTimeEnd").val(),
                carType: $("#inquiryAdd_carType option:selected").text(),
                carLength: $("#inquiryAdd_carLength option:selected").text()
            };
            let inquiryChildListPractical = {
                startProvince: this.$scope.startProvince,
                startCity: this.$scope.startCity,
                startArea: this.$scope.startArea,
                startAddress: this.$scope.startAddress,
                endProvince: this.$scope.endProvince,
                endCity: this.$scope.endCity,
                endArea: this.$scope.endArea,
                endAddress: this.$scope.endAddress,
                plannedArrivalTime: $("#dataTime").val(),
                plannedDeliveryTime: $("#dataTimeEnd").val(),
                mileage: this.$scope.mileage,
                goodsTypeId: this.$scope.goodsTypeId,
                goodsTypeName: $("#inquiryAdd_goodsTypeName option:selected").text(),
                goodsId: this.$scope.goodsId,
                // goodsName: $("#inquiryAdd_goodsName option:selected").text(),
                goodsName: $("#goodsId_value").val(),
                goodsQuantity: this.$scope.goodsQuantity,
                goodsQuantityUnit: this.$scope.goodsQuantityUnit,
                carType: this.$scope.carType,
                carLength: this.$scope.carLength,
                carrierCategory: this.$scope.carrierCategory,
                loadingEffect: this.$scope.loadingEffect,
                remarks: this.$scope.remarks,
            };
            this.inquiryChildListData.push(inquiryChildListPractical);
            this.$scope.inquiryChildViewList.push(inquiryChildListData);//子询价单列表数据
        }

            
        this.$scope.inquiryChildViewList.length > 0 ? this.$scope.isInquiryDis = true : this.$scope.isInquiryDis = false;
    }

    /**
     * 添加/复制询价发布
     */
    inquirySave = () => {
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return; };
        if (this.$scope.inquiryChildViewList.length < 1) {
            this.$ngBootbox.alert("子询价单至少为一条");
            return;
        }
        if (!$("#inquiryForm").valid()) { return; }
        this.$scope.saveDis = true;
        var attachmentArr = [];
        this.uploaderList.forEach((item, index) => {
            // attachmentArr.push([item.attachmentId,item.attachmentName,item.attachmentPath,item.attachmentTag]);   
            attachmentArr.push([item.attachmentName, item.attachmentPath]);

        });
        this.$ngBootbox.confirm("确认询价发布吗？").then(() => {
            this.clientService.getDetail(this.$scope.clientId).then((res) => {
                let clientCornet = res.data.cornet;
                this.inquiryService.add(
                    Number(this.$scope.urgency),
                    this.$scope.urgencyUnit,
                    this.$scope.businessOfficerId,
                    this.$scope.plannerName,
                    $("#inquiryAdd_businessOfficerId option:selected").text(),
                    this.$scope.customerServiceOfficerId,
                    $("#inquiryAdd_customerServiceOfficerId option:selected").text(),
                    this.$scope.dispatchOfficerId,
                    $("#inquiryAdd_dispatchOfficerId option:selected").text(),
                    this.$scope.clientId,
                    $("#client_value").val(),
                    this.$scope.consignorId,
                    this.$scope.content,
                    this.inquiryChildListData,
                    //this.uploaderList,
                    attachmentArr,
                    this.$scope.attachmentDetails,
                    clientCornet
                ).then((res) => {
                    if(this.$state.current.name.indexOf('New')>-1){
                        this.$state.go('app.inquiry.inquiryListNew', { name: 'inquiry' });
                    }else{
                        this.$state.go('app.inquiry.inquiryList', { name: 'inquiry' });
                    }
                });
            });
        }, () => {
            this.$scope.saveDis = false;
        });
    }

    /**
     * 编辑询价发布
     */
    inquirySaveEdit = () => {
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return; };
        var attachmentArr = [];
        this.uploaderList.forEach((item, index) => {
            // attachmentArr.push([item.attachmentId,item.attachmentName,item.attachmentPath,item.attachmentTag]);   
            attachmentArr.push([item.attachmentName, item.attachmentPath]);

        });
        this.$ngBootbox.confirm("确认询价发布吗？").then(() => {
            let inquiryChildListPractical = {
                startProvince: this.$scope.startProvince,
                startCity: this.$scope.startCity,
                startArea: this.$scope.startArea,
                startAddress: this.$scope.startAddress,
                endProvince: this.$scope.endProvince,
                endCity: this.$scope.endCity,
                endArea: this.$scope.endArea,
                endAddress: this.$scope.endAddress,
                plannedArrivalTime: $("#dataTime").val(),
                plannedDeliveryTime: $("#dataTimeEnd").val(),
                mileage: this.$scope.mileage,
                goodsTypeId: this.$scope.goodsTypeId,
                goodsTypeName: $("#inquiryAdd_goodsTypeName option:selected").text(),
                goodsId: this.$scope.goodsId,
                // goodsName: $("#inquiryAdd_goodsName option:selected").text(),
                goodsName: $("#goodsId_value").val(),
                goodsQuantity: this.$scope.goodsQuantity,
                goodsQuantityUnit: this.$scope.goodsQuantityUnit,
                carType: this.$scope.carType,
                carLength: this.$scope.carLength,
                carrierCategory: this.$scope.carrierCategory,
                loadingEffect: this.$scope.loadingEffect,
                remarks: this.$scope.remarks,
            };
            this.inquiryChildListData.push(inquiryChildListPractical);
            this.clientService.getDetail(this.$scope.clientId).then((res) => {
                let clientCornet = res.data.cornet;
                this.inquiryService.edit(
                    this.inquiryId,
                    Number(this.$scope.urgency),
                    this.$scope.urgencyUnit,
                    this.$scope.businessOfficerId,
                    this.$scope.plannerName,
                    $("#inquiryAdd_businessOfficerId option:selected").text(),
                    this.$scope.customerServiceOfficerId,
                    $("#inquiryAdd_customerServiceOfficerId option:selected").text(),
                    this.$scope.dispatchOfficerId,
                    $("#inquiryAdd_dispatchOfficerId option:selected").text(),
                    this.$scope.clientId,
                    $("#client_value").val(),
                    this.$scope.consignorId,
                    this.$scope.content,
                    this.inquiryChildListData,
                    //this.uploaderList,
                    attachmentArr,
                    this.$scope.attachmentDetails,
                    clientCornet
                ).then((res) => {
                    if(this.$state.current.name.indexOf('New')>-1){
                        this.$state.go('app.inquiry.inquiryListNew', { name: 'inquiry' });
                    }else{
                        this.$state.go('app.inquiry.inquiryList', { name: 'inquiry' });
                    }
                });
            });
        });
    }

    /**
     * 紧急程度单位下拉
     */
    loadUrgencyUnitDropDown = () => {
        this.$scope.urgencyUnitDropDown = this.valueService.getUrgencyUnitList().data;
    }

    /**
     * 商务专员下拉
     */
    loadBusinessOfficerDropDown = () => {
        this.employeeService.getbytype(CommissionerType.BusinessAffairs.toString(), 0, -1).then(result => {
            this.$scope.businessOfficerDropDown = result.data.data;
        });
    }

    /**
     * 客服专员下拉
     */
    loadServiceOfficerIdDropDown = () => {
        this.employeeService.getbytype(CommissionerType.CustomerService.toString(), 0, -1).then(result => {
            this.$scope.serviceOfficerIdDropDown = result.data.data;
        });
    }

    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown = () => {
        this.employeeService.getbytype(CommissionerType.Dispatch.toString(), 0, -1).then(result => {
            this.$scope.dispatchOfficerDropDown = result.data.data;
        });
    }
    /**
     * 货物数量单位下拉
     */
    loadGoodsQuantityUnitDropDown = () => {
        this.$scope.goodsQuantityUnitDropDown = this.valueService.getGoodQuantityUnitList().data;
    }

    /**
     * 车型下拉
     */
    loadCarTypeDropDown = () => {
        this.$scope.carTypeDropDown = this.valueService.getCarTypeList().data;
    }

    /**
     * 车长下拉
     */
    loadCarLengthDropDown = () => {
        this.$scope.carLengthDropDown = this.valueService.getCarLengthList().data;
    }

    /**
     * 承运方式下拉
     */
    loadCarrierCategoryDropDown = () => {
        this.$scope.carrierCategoryDropDown = this.valueService.getCarrierWayList().data;
    }

    /**
     * 选择上传文件后
     */
    onSuccessItem(fileId: string, fileName: string, filePath: string, tag: number, file?: any): void {
        file.fileId = filePath;
        let attachment = {
            attachmentId: fileId,
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: ""
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 删除子询价单
     */
    delInquiryChild = (index: number) => {
        this.inquiryChildListData.splice(index - 1, 1);
        this.$scope.inquiryChildViewList.splice(index - 1, 1);
        for (var i = 0; i < this.$scope.inquiryChildViewList.length; i++) {
            this.$scope.inquiryChildViewList[i].index = i + 1;
        }

        this.$scope.inquiryChildViewList.length > 0 ? this.$scope.isInquiryDis = true : this.$scope.isInquiryDis = false;
    };
}

angular.module('tms').controller("inquiryAddController", ["$scope", "customerRepresentativeService", "$state", "$location", "$http", "inquiryService", "clientService", "employeeService", "goodsService", "goodsTypeService",
    "areaService", "valueService", "routeService", "$stateParams", "$ngBootbox", "commonService","$q","ossUploaderService", InquiryAddController]);